package com.qingxin.cms.action;

import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qingxin.cms.entity.QxNotices;
import com.qingxin.cms.entity.QxNoticesAttachments;
import com.qingxin.cms.service.QxNoticesAttachmentsService;
import com.qingxin.cms.service.QxNoticesService;
import com.qingxin.sys.action.BaseAction;
import com.qingxin.sys.entity.MyUploadFile;
import com.qingxin.sys.entity.MyUploadFileParam;
import com.qingxin.sys.entity.MyUploadFiles;
import com.qingxin.sys.mvc.PageParam;
import com.qingxin.sys.mvc.SysError;
import com.qingxin.sys.support.UploadFile;
import com.qingxin.sys.utils.DateUtil;

@Controller
@RequestMapping("/cms/qxNotices")
public class QxNoticesAction extends BaseAction {

	@Autowired
	private QxNoticesService noticesService;
	@Autowired
	private QxNoticesAttachmentsService attachmentsService;
	
	
	@RequestMapping(value="/getLastestNotices.json", method={RequestMethod.GET, RequestMethod.POST})
	@ResponseBody
	public Object getLastestNotices(){
		QxNotices notices = noticesService.getLastest();
		Map<String, String> map = new HashMap<>();
		if(notices != null){
			map.put("id", notices.getNoticeid());
		}
		return map;
	}
	
	
	@RequestMapping(value="/list.do", method=RequestMethod.GET)
	public String list(ModelMap model, HttpServletRequest request){
		return "trace/cms/qxNotices/qxNotices_list";
	}
	
	
	@RequestMapping(value="/listdata.json", method={RequestMethod.GET, RequestMethod.POST})
	@ResponseBody
	public Object listdata(String search, PageParam pageParam, HttpServletRequest request){
		return noticesService.list(search, pageParam, request);
	}
	
	
	/**
	 * 添加
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/add.do", method=RequestMethod.GET)
	public String add(ModelMap model, HttpServletRequest request){
		model.put("pubUser", getLoginUser(request).getLogin_name());
		model.put("pubDate", DateUtil.fmtDate(new Date()));
		
		return "/trace/cms/qxNotices/qxNotices_add";
	}
	
	
	/**
	 * 保存
	 * @param notices
	 * @param result
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/save.json", method=RequestMethod.POST)
	@ResponseBody
	public Object save(@Valid QxNotices notices, BindingResult result, HttpServletRequest request){
		if(result.hasErrors()){
			return getSysErrors(result);
		}
		
		MyUploadFileParam uploadFileParam = new MyUploadFileParam("attachment", request);
		
		SysError info = noticesService.add(notices, uploadFileParam, getLoginUser(request), request);
		return info;
	}
	
	
	/**
	 * 修改
	 * @param model
	 * @param noticeid
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/edit.do", method=RequestMethod.GET)
	public String edit(ModelMap model, String noticeid, HttpServletRequest request){
		QxNotices notices = noticesService.get(noticeid);
		model.put("notices", notices);
	
		List<QxNoticesAttachments> list = attachmentsService.list(noticeid);
		MyUploadFiles attachments = new MyUploadFiles();
		for(QxNoticesAttachments att : list){
			attachments.addFile(new MyUploadFile(att.getId(), att.getFilePath(), att.getFilePath(), att.getFileName(), null));
		}
		model.put("attachments", attachments);
		return "trace/cms/qxNotices/qxNotices_edit";
	}
	
	
	/**
	 * 保存修改
	 * @param notices
	 * @param result
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/update.json", method=RequestMethod.POST)
	@ResponseBody
	public Object update(@Valid QxNotices notices, BindingResult result, HttpServletRequest request){
		if(result.hasErrors()){
			return getSysErrors(result);
		}
		
		MyUploadFileParam uploadFileParam = new MyUploadFileParam("attachment", request);
		
		return noticesService.update(notices, uploadFileParam, getLoginUser(request), request);
	}
	
	
	/**
	 * 查看
	 * @param model
	 * @param noticeid
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/view.do", method=RequestMethod.GET)
	public String view(ModelMap model, String noticeid, HttpServletRequest request){
		QxNotices notices = noticesService.get(noticeid);
		model.put("notices", notices);
	
		List<QxNoticesAttachments> attachments = attachmentsService.list(noticeid);
		model.put("attachments", attachments);
		
		return "trace/cms/qxNotices/qxNotices_view";
	}
	
	
	/**
	 * 删除文章
	 * @param noticeid
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/delete.json", method=RequestMethod.POST)
	@ResponseBody
	public Object delete(String[] noticeid, HttpServletRequest request){
		return noticesService.delete(noticeid, request);
	}
	
	
	/**
	 * 下载附件
	 * @param id
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/downloadAttach.do", method=RequestMethod.GET)
	public String downloadAttach(String id, HttpServletRequest request, HttpServletResponse response) throws Exception {
		QxNoticesAttachments attach = attachmentsService.get(id);
		System.out.println("attach="+attach);
		if(attach == null){
			response.sendError(HttpServletResponse.SC_NOT_FOUND);
			return null;
		}
		File file = new File(UploadFile.getFilerootRealpath(UploadFile.DIR_CMS_ATTACHMENT+"/"+attach.getFilePath()));
		System.out.println("file="+file.getCanonicalPath());
		if(!file.isFile()){
			response.sendError(HttpServletResponse.SC_NOT_FOUND);
			return null;
		}
		
		downloadFile(file, attach.getFileName(), response);
		return null;
	}
	
	
}
